<template>
<div class="login">
    <div class="container">
        <img class="avatar" src="../assets/avatar.jpg" alt="">
        <el-form :model="loginForm" 
             :rules="rules"
             class="demo-ruleForm"
             ref="loginForm"
             style="">
            <el-form-item prop="username">
                <el-input v-model="loginForm.username"
                          placeholder="请输入用户名"
                          prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password"
                          placeholder="请输入密码"
                          prefix-icon="el-icon-key"></el-input>
            </el-form-item>
            <el-row>
                <el-button class="login-btn" 
                           type="primary"
                           @click="login">登录</el-button>
            </el-row>
        </el-form>
    </div>
</div>
</template>

<script>
import { userlogin } from "../apis/user";
export default {
data () {
    return {
        loginForm:{
            username:'13172964309',
            password:'chang0608'
        },
        rules: {
            username: [
                { required: true, message: '请输入手机号', trigger: 'blur' },
                { pattern: /^1[35789]\d{9}$/, message: '请输入11位手机号', trigger: 'blur' },
            ],
            password: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { pattern: /^[a-zA-Z0-9]{3,16}$/, message: '请输入3~16位密码', trigger: 'blur' },
            ]
        }
    }
},
methods: {
    login(){
        this.$refs.loginForm.validate((valid) => {
            if (valid) {
                userlogin(this.loginForm)
                .then(res => {
                    console.log(res);
                    if (res.data.message == "登录成功") {
                        this.$router.push({path:'/index'})
                        localStorage.setItem('heimaHoutai', res.data.data.token)
                        localStorage.setItem('heimaUser', JSON.stringify(res.data.data.user))
                    }
                })
            }else{
                this.$message({
                    message:'用户输入内容不合法',
                    type:'warning'
                });
                return false
            }
        })
    }
}
}
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>